<!DOCTYPE html>
<html lang="zh_cn">
<head>
          <title>来玩魔王的咚</title>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta charset="utf-8" />
        <!-- twitter card metadata -->
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="/images/mowang.png">
<meta name="twitter:site" content="">
<meta name="twitter:title" content="适配器模式">
<meta name="twitter:description" content="<p>适配器基于OOP方式的实现；Go语言中更轻量级的适配器实现：通过为函数定义方法</p>">
        <!-- OG Tags -->
<meta property="og:url" content="/gopl-adapter-pattern.html"/>
<meta property="og:title" content="来玩魔王的咚 | 适配器模式" />
<meta property="og:description" content="<p>适配器基于OOP方式的实现；Go语言中更轻量级的适配器实现：通过为函数定义方法</p>" />
        <!-- favicon -->
        <link rel="icon" type="image/png" href="/images/mowang.png">
        <!-- moment.js for date formatting -->
        <script src="/theme/js/moment.js"></script>
        <!-- css -->
        <link rel="stylesheet" type="text/css" href="/theme/css/main.css" />
        <!-- 左边的menu，如果页面高度不够，就跟着滚动，否则文章分类显示不全 -->
        <link rel="stylesheet" type="text/css" href="/theme/css/mycss/menu.css" />
		<script>
			
                /*! grunt-grunticon Stylesheet Loader - v2.1.2 | https://github.com/filamentgroup/grunticon | (c) 2015 Scott Jehl, Filament Group, Inc. | MIT license. */
    
    (function(e){function t(t,n,r,o){"use strict";function a(){for(var e,n=0;u.length>n;n++)u[n].href&&u[n].href.indexOf(t)>-1&&(e=!0);e?i.media=r||"all":setTimeout(a)}var i=e.document.createElement("link"),l=n||e.document.getElementsByTagName("script")[0],u=e.document.styleSheets;return i.rel="stylesheet",i.href=t,i.media="only x",i.onload=o||null,l.parentNode.insertBefore(i,l),a(),i}var n=function(r,o){"use strict";if(r&&3===r.length){var a=e.navigator,i=e.Image,l=!(!document.createElementNS||!document.createElementNS("http://www.w3.org/2000/svg","svg").createSVGRect||!document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image","1.1")||e.opera&&-1===a.userAgent.indexOf("Chrome")||-1!==a.userAgent.indexOf("Series40")),u=new i;u.onerror=function(){n.method="png",n.href=r[2],t(r[2])},u.onload=function(){var e=1===u.width&&1===u.height,a=r[e&&l?0:e?1:2];n.method=e&&l?"svg":e?"datapng":"png",n.href=a,t(a,null,null,o)},u.src="",document.documentElement.className+=" grunticon"}};n.loadCSS=t,e.grunticon=n})(this);(function(e,t){"use strict";var n=t.document,r="grunticon:",o=function(e){if(n.attachEvent?"complete"===n.readyState:"loading"!==n.readyState)e();else{var t=!1;n.addEventListener("readystatechange",function(){t||(t=!0,e())},!1)}},a=function(e){return t.document.querySelector('link[href$="'+e+'"]')},c=function(e){var t,n,o,a,c,i,u={};if(t=e.sheet,!t)return u;n=t.cssRules?t.cssRules:t.rules;for(var l=0;n.length>l;l++)o=n[l].cssText,a=r+n[l].selectorText,c=o.split(");")[0].match(/US\-ASCII\,([^"']+)/),c&&c[1]&&(i=decodeURIComponent(c[1]),u[a]=i);return u},i=function(e){var t,o,a;o="data-grunticon-embed";for(var c in e)if(a=c.slice(r.length),t=n.querySelectorAll(a+"["+o+"]"),t.length)for(var i=0;t.length>i;i++)t[i].innerHTML=e[c],t[i].style.backgroundImage="none",t[i].removeAttribute(o);return t},u=function(t){"svg"===e.method&&o(function(){i(c(a(e.href))),"function"==typeof t&&t()})};e.embedIcons=i,e.getCSS=a,e.getIcons=c,e.ready=o,e.svgLoadedCallback=u,e.embedSVG=u})(grunticon,this);
                
                grunticon(["/theme/css/icons.data.svg.css", "/theme/css/icons.data.png.css", "/theme/css/icons.fallback.css"]);
            </script>
        <noscript><link href="/theme/css/icons.fallback.css" rel="stylesheet"></noscript>
        <!-- menu toggle javascript -->
        <script type="text/javascript">
            document.addEventListener("DOMContentLoaded", initMenu);
            
            function initMenu(){
                var menu = document.getElementById("menu");
                var menulink = document.getElementById("menu-link");
                menulink.addEventListener("click", function toggleMenu(){
                        window.event.preventDefault();
                        menulink.classList.toggle('active');
                        menu.classList.toggle('active');              
                    });
            };
        </script>
        <!-- 不蒜子 -->
        <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

    <meta name="description" content="<p>适配器基于OOP方式的实现；Go语言中更轻量级的适配器实现：通过为函数定义方法</p>" />

    <meta name="tags" content="gopl" />
  <!-- 替换部分base的样式，看文章时，要再宽一点，右边有很多空间可以撑开 -->
  <link rel="stylesheet" type="text/css" href="/theme/css/mycss/article.css" />

</head>
<body>
    <div role="banner" id="masthead">
        <header>
            <a href="/"><img src="/images/mowang.png" alt="McManus Logo"></a>
                <h1>来玩魔王的咚@骑士救兵</h1>
            <a href="#menu" id="menu-link">more stuff</a>
            <nav id="menu">
                <ul>
                        <li><a href="/tags">tags</a></li>
                            <li><a href="/category/cloud.html">Cloud</a></li>
                            <li><a href="/category/docker.html">Docker</a></li>
                            <li class="active"><a href="/category/go.html">Go</a></li>
                            <li><a href="/category/linux.html">Linux</a></li>
                            <li><a href="/category/python.html">Python</a></li>
                            <li><a href="/category/xue-xi-bi-ji.html">学习笔记</a></li>
                            <li><a href="/category/yun-wei-zi-dong-hua.html">运维自动化</a></li>
                </ul>
            </nav>
        </header>
    </div>
        <div class="page" role="main">
  <div class="article" role="article">
    <article>
        <footer>
            <a name="top"></a>
            <p>
              <time datetime=" 2020-08-04 17:10:00+08:00">
                <script>document.write(moment('2020-08-04 17:10:00+08:00').format('LL'));</script>
              </time>
              ~
              <time datetime=" 2020-08-04 17:10:00+08:00">
                <script>document.write(moment('2020-08-04 17:10:00+08:00').format('LL'));</script>
              </time>
            </p>
        </footer>
        <header>
          <h2>
            适配器模式
          </h2>
        </header>
      <div class="content">
         <div class="toc">
<ul>
<li><a href="#shi-pei-qi">适配器</a><ul>
<li><a href="#oop-de-shi-xian">OOP的实现</a></li>
<li><a href="#wei-han-shu-lei-xing-ding-yi-fang-fa">为函数类型定义方法</a></li>
<li><a href="#wan-zheng-de-shi-li-dai-ma">完整的示例代码</a></li>
</ul>
</li>
</ul>
</div>
<h3 id="shi-pei-qi"><a class="toclink" href="#shi-pei-qi">适配器</a></h3>
<p>适配器模式是设计模式中的一种：将一个类的接口转换成客户希望的另一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。  </p>
<h4 id="oop-de-shi-xian"><a class="toclink" href="#oop-de-shi-xian">OOP的实现</a></h4>
<p>在一般的向对象的语言里，可能是定义一个适配器类来实现的。Go 自然也是可以的。<br>
比如已经有了一个自己的类，并且实现了一些方法：</p>
<div class="highlight"><pre><span></span><code><span class="kd">type</span><span class="w"> </span><span class="nx">People</span><span class="w"> </span><span class="kd">struct</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">name</span><span class="w"> </span><span class="kt">string</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">p</span><span class="w"> </span><span class="o">*</span><span class="nx">People</span><span class="p">)</span><span class="w"> </span><span class="nx">Greet</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">&quot;Hello, I am %s.\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">p</span><span class="p">.</span><span class="nx">name</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>现在有一个接口，下面是接口的定义和一个调用该接口的函数：</p>
<div class="highlight"><pre><span></span><code><span class="kd">type</span><span class="w"> </span><span class="nx">Hello</span><span class="w"> </span><span class="kd">interface</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">SayHello</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">SayHello</span><span class="p">(</span><span class="nx">s</span><span class="w"> </span><span class="nx">Hello</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">s</span><span class="p">.</span><span class="nx">SayHello</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>问题来了，Peopel并没有实现接口的方法，所以不能直接传递给 SayHello 函数。  </p>
<h5 id="jie-jue-fang-an-yi"><a class="toclink" href="#jie-jue-fang-an-yi">解决方案一</a></h5>
<p>直接为People再写一个方法就解决了。但是如果这个People是别的包里的类型，就不能这么做了。  </p>
<h5 id="jie-jue-fang-an-er"><a class="toclink" href="#jie-jue-fang-an-er">解决方案二</a></h5>
<p>为原来的People定义一个别名类型，然后定义新的别名类型的方法来满足接口。实现后大概是下面这样的：</p>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">p2</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">PeopleSayHello</span><span class="p">(</span><span class="nx">p1</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="nx">SayHello</span><span class="p">(</span><span class="o">&amp;</span><span class="nx">p2</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// 适配器</span><span class="w"></span>
<span class="kd">type</span><span class="w"> </span><span class="nx">PeopleSayHello</span><span class="w"> </span><span class="nx">People</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">p</span><span class="w"> </span><span class="o">*</span><span class="nx">PeopleSayHello</span><span class="p">)</span><span class="w"> </span><span class="nx">SayHello</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">p1</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">People</span><span class="p">(</span><span class="o">*</span><span class="nx">p</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="nx">p1</span><span class="p">.</span><span class="nx">Greet</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>由于类型转换之后的新的类型是无法取地址的，所以必须要引入一个临时变量。在main函数中多出这么一行也不是很好。  </p>
<h5 id="jie-jue-fang-an-san"><a class="toclink" href="#jie-jue-fang-an-san">解决方案三</a></h5>
<p>定义一个新的结构体。这个是适配器模式的标准做法，网上也有很多类似的文章：</p>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">SayHello</span><span class="p">(</span><span class="o">&amp;</span><span class="nx">PeopleSayHello</span><span class="p">{</span><span class="o">&amp;</span><span class="nx">p1</span><span class="p">})</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// 适配器</span><span class="w"></span>
<span class="kd">type</span><span class="w"> </span><span class="nx">PeopleSayHello</span><span class="w"> </span><span class="kd">struct</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">*</span><span class="nx">People</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">p</span><span class="w"> </span><span class="o">*</span><span class="nx">PeopleSayHello</span><span class="p">)</span><span class="w"> </span><span class="nx">SayHello</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">p</span><span class="p">.</span><span class="nx">Greet</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>这里的方法实现也可以完全自定义。这里只是简单的调用了一个原结构体里的方法，对于这种简单的情况，有更轻量级的方式来适配接口。  </p>
<h4 id="wei-han-shu-lei-xing-ding-yi-fang-fa"><a class="toclink" href="#wei-han-shu-lei-xing-ding-yi-fang-fa">为函数类型定义方法</a></h4>
<p>Go 语言的接口机制有一些不常见的特性。就是作为一个函数类型，还可以拥有自己的方法。<br>
先看下面的实现：</p>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">SayHello</span><span class="p">(</span><span class="nx">PeopleSayHello</span><span class="p">(</span><span class="nx">p1</span><span class="p">.</span><span class="nx">Greet</span><span class="p">))</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// 适配器</span><span class="w"></span>
<span class="kd">type</span><span class="w"> </span><span class="nx">PeopleSayHello</span><span class="w"> </span><span class="kd">func</span><span class="p">()</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">f</span><span class="w"> </span><span class="nx">PeopleSayHello</span><span class="p">)</span><span class="w"> </span><span class="nx">SayHello</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">f</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>表达式<code>PeopleSayHello(p1.Greet)</code>并不是一个并不是一个函数调用，而是一个类型转换。  </p>
<p><code>p1.Greet</code>是一个方法值，就是如下类型的一个值：</p>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">SayHello</span><span class="p">()</span><span class="w"></span>
</code></pre></div>

<p><em>可以简单的在这里就把接口值就当做是一个函数值。</em><br>
新的类型中定义了 SayHello 方法，现在满足接口了，而这个方法就是调用函数本身，所以这里的 PeopleSayHello 就是一个让函数值满足接口的一个适配器。<br>
这里用到的是方法值，其实本质上就是为一个函数值定义方法来适配接口。所以，对于方法表达式或函数，也是同样适用的。<br>
在标准库的 net\/http 中也有类似的用法，就是下面的 HandlerFunc 类型：</p>
<div class="highlight"><pre><span></span><code><span class="kn">package</span><span class="w"> </span><span class="nx">http</span><span class="w"></span>
<span class="kd">type</span><span class="w"> </span><span class="nx">HandlerFunc</span><span class="w"> </span><span class="kd">func</span><span class="p">(</span><span class="nx">ResponseWriter</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="nx">Request</span><span class="p">)</span><span class="w"></span>
<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">f</span><span class="w"> </span><span class="nx">HandlerFunc</span><span class="p">)</span><span class="w"> </span><span class="nx">ServeHTTP</span><span class="p">(</span><span class="nx">w</span><span class="w"> </span><span class="nx">ResponseWriter</span><span class="p">,</span><span class="w"> </span><span class="nx">r</span><span class="w"> </span><span class="o">*</span><span class="nx">Request</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">f</span><span class="p">(</span><span class="nx">w</span><span class="p">,</span><span class="w"> </span><span class="nx">r</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<h4 id="wan-zheng-de-shi-li-dai-ma"><a class="toclink" href="#wan-zheng-de-shi-li-dai-ma">完整的示例代码</a></h4>
<p>最后一个例子的完整的示例代码。其他几个例子也只是 main 函数中的调用，和适配器实现部分的代码不同：</p>
<div class="highlight"><pre><span></span><code><span class="kn">package</span><span class="w"> </span><span class="nx">main</span><span class="w"></span>

<span class="kn">import</span><span class="w"> </span><span class="s">&quot;fmt&quot;</span><span class="w"></span>

<span class="kd">type</span><span class="w"> </span><span class="nx">People</span><span class="w"> </span><span class="kd">struct</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">name</span><span class="w"> </span><span class="kt">string</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">p</span><span class="w"> </span><span class="o">*</span><span class="nx">People</span><span class="p">)</span><span class="w"> </span><span class="nx">Greet</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">&quot;Hello, I am %s.\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">p</span><span class="p">.</span><span class="nx">name</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">var</span><span class="w"> </span><span class="nx">p1</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">People</span><span class="p">{</span><span class="s">&quot;Adam&quot;</span><span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">init</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Print</span><span class="p">(</span><span class="s">&quot;init: &quot;</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="nx">p1</span><span class="p">.</span><span class="nx">Greet</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// 接口</span><span class="w"></span>
<span class="kd">type</span><span class="w"> </span><span class="nx">Hello</span><span class="w"> </span><span class="kd">interface</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">SayHello</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">SayHello</span><span class="p">(</span><span class="nx">s</span><span class="w"> </span><span class="nx">Hello</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">s</span><span class="p">.</span><span class="nx">SayHello</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">SayHello</span><span class="p">(</span><span class="nx">PeopleSayHello</span><span class="p">(</span><span class="nx">p1</span><span class="p">.</span><span class="nx">Greet</span><span class="p">))</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// 适配器</span><span class="w"></span>
<span class="kd">type</span><span class="w"> </span><span class="nx">PeopleSayHello</span><span class="w"> </span><span class="kd">func</span><span class="p">()</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">f</span><span class="w"> </span><span class="nx">PeopleSayHello</span><span class="p">)</span><span class="w"> </span><span class="nx">SayHello</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">f</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
      </div>
      <div class="back-to-top">
        <a href="/">HOME</a>
        <a href="#top">TOP</a>
      </div>
    </article>
  </div>
<!-- end article -->
<!-- 页面往下滚动一段之后才会显示TOC -->
<script>
  window.onscroll = function() {
    var tocbox = document.getElementsByClassName('toc')[0];
    var osTop = document.documentElement.scrollTop || document.body.scrollTop;
    var osWidth = document.documentElement.scrollWidth || document.body.scrollWidth;
    // console.log(osTop)
    if (osTop>300 && osWidth>865) {
      tocbox.style.display = "block"
    }
    if (osTop<300 || osWidth<865) {
      tocbox.style.display = "none"
    }
  }
</script>
                <footer>
                    <div class="icons">
                    </div>
                    <span id="busuanzi_container_page_pv" style="padding: 10px">本文阅读量<span id="busuanzi_value_page_pv"></span>次</span>
                    <span id="busuanzi_container_site_pv" style="padding: 10px">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
                    <span id="busuanzi_container_site_uv" style="padding: 10px">本站总访客数<span id="busuanzi_value_site_uv"></span>人</span>
                    <p>© <script>document.write(moment().format('YYYY'));</script> 749B</p>
                </footer>
        </div>
</body>
</html>